// pages/firework/analysis/add/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), 
common_Js = require("../../../../component/common.js")
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        workId:0,
        submitState:false,
        isShowPop:false,
        analysis:{
            fireWorkId:'',
            recordType: 1,
            pointName:'',
            analysisData:'',
            harmfulGas:'',
            harmfulGasCriteria:'',
            harmfulData:'',
            fireGas:'',
            fireGasCriteria:'',
            fireGasData:'',
            analysisPart:'',
            analysisTime:'',
            oVolum:'',
            opUser:'',
            otherSpecial:'',
            dangerRemark:'',
        },
        minDate:new Date(2023, 10, 1).getTime(),
        currentDate:new Date().getTime(),
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options.workId){
          this.data.workId = parseInt(options.workId)
          this.data.analysis.fireWorkId = parseInt(options.workId)
        }
    }, 
  bindInput(e){
    var key = e.currentTarget.dataset.key;
    if(key == 'point_name'){
      this.data.analysis.pointName = e.detail;
    } else if(key == 'analysis_data'){
      this.data.analysis.analysisData = e.detail;
    } else if(key == 'harmful_gas'){
      this.data.analysis.harmfulGas = e.detail;
    } else if(key == 'harmful_gas_criteria'){
      this.data.analysis.harmfulGasCriteria = e.detail;
    } else if(key == 'harmful_gas_data'){
      this.data.analysis.harmfulGasData = e.detail;
    } else if(key == 'fire_gas'){
      this.data.analysis.fireGas = e.detail;
    } else if(key == 'fire_gas_criteria'){
      this.data.analysis.fireGasCriteria = e.detail;
    } else if(key == 'fire_gas_data'){
      this.data.analysis.fireGasData = e.detail;
    } else if(key == 'analysis_part'){
      this.data.analysis.analysisPart = e.detail;
    } else if(key == 'analysis_time'){
      this.data.analysis.analysisTime = e.detail;
    } else if(key == 'o_volum'){
      this.data.analysis.oVolum = e.detail;
    } else if(key == 'op_user'){
      this.data.analysis.opUser = e.detail;
    }else if(key == 'other_special'){
      this.data.analysis.otherSpecial = e.detail;
    }else if(key == 'danger_remark'){
      this.data.analysis.dangerRemark = e.detail;
    }
  },
  showPop(e){
    this.setData({
      isShowPop: true,
    })
  },
  onConfirm(e){
    this.setData({
      isShowPop: false,
    })
    console.log('onConfirm', e);

    var time = common_Js.formatTime1(new Date(e.detail));
    console.log('time', time);
    this.data.analysis.analysisTime = time;
  },
  onCancle(){
    this.setData({
      isShowPop: false,
    })
  },
  getResult(time, type) {
    const date = new Date(time);
    switch (type) {
      case 'datetime':
        return date.toLocaleString();
      case 'date':
        return date.toLocaleDateString();
      case 'year-month':
        return `${date.getFullYear()}/${date.getMonth() + 1}`;
      case 'time':
        return time;
      default:
        return '';
    }
  },
  submitOp(){
    var analysis = this.data.analysis;
    if(analysis.oVolum == ''){
      Toast('请输入氧气含量');return ;
    }
    // if(analysis.analysisTime == ''){
    //   Toast('请选择分析时间');return ;
    // }
    // if(analysis.opUser == ''){
    //   Toast('请输入分析人');return ;
    // }
    this.onHttpSubmit();
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpSubmit: function (e) {
    var that = this;
    fetchApi.httpPost('/fire/work/add/analysis', {
        analysis:that.data.analysis,
        work_id: that.data.workId
    }, function (res) {
      console.log("发起申请响应数据", res)
      if (res.data.code == 0) {
          Toast.success(res.data.msg);
          setTimeout(function(){
              wx.navigateBack({
                  delta: -1
              })
          },500)
      }else{
        Toast.fail(res.data.msg);
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
})